home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SAMPLES / LINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-19  |  4.2 KB  |  219 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <GL/glut.h>
  29.  
  30.  
  31. #define CI_OFFSET 16
  32.  
  33.  
  34. GLenum rgb, doubleBuffer, windType;
  35.  
  36. GLenum mode1, mode2;
  37. GLint size;
  38. float pntA[3] = {
  39.     -160.0, 0.0, 0.0
  40. };
  41. float pntB[3] = {
  42.     -130.0, 0.0, 0.0
  43. };
  44. float pntC[3] = {
  45.     -40.0, -50.0, 0.0
  46. };
  47. float pntD[3] = {
  48.     30.0, 60.0, 0.0
  49. };
  50.  
  51.  
  52. #include "tkmap.c"
  53.  
  54. static void Init(void)
  55. {
  56.     GLint i;
  57.  
  58.     glClearColor(0.0, 0.0, 0.0, 0.0);
  59.  
  60.     glLineStipple(1, 0xF0E0);
  61.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  62.  
  63.     if (!rgb) {
  64.     for (i = 0; i < 16; i++) {
  65.         glutSetColor(i+CI_OFFSET, i/15.0, i/15.0, 0.0);
  66.     }
  67.     }
  68.  
  69.     mode1 = GL_FALSE;
  70.     mode2 = GL_FALSE;
  71.     size = 1;
  72. }
  73.  
  74. static void Reshape(int width, int height)
  75. {
  76.  
  77.     glViewport(0, 0, (GLint)width, (GLint)height);
  78.  
  79.     glMatrixMode(GL_PROJECTION);
  80.     glLoadIdentity();
  81.     gluOrtho2D(-175, 175, -175, 175);
  82.     glMatrixMode(GL_MODELVIEW);
  83. }
  84.  
  85. static void Key(unsigned char key, int x, int y)
  86. {
  87.  
  88.     switch (key) {
  89.       case 27:
  90.     exit(1);
  91.       case '1':
  92.     mode1 = !mode1;
  93.     break;
  94.       case '2':
  95.     mode2 = !mode2;
  96.     break;
  97.       case 'W':
  98.     size++;
  99.     break;
  100.       case 'w':
  101.     size--;
  102.     if (size < 1) {
  103.         size = 1;
  104.     }
  105.     break;
  106.       default:
  107.     return;
  108.     }
  109.  
  110.     glutPostRedisplay();
  111. }
  112.  
  113. static void Draw(void)
  114. {
  115.     GLint ci, i;
  116.  
  117.     glClear(GL_COLOR_BUFFER_BIT);
  118.  
  119.     glLineWidth(size);
  120.  
  121.     if (mode1) {
  122.     glEnable(GL_LINE_STIPPLE);
  123.     } else {
  124.     glDisable(GL_LINE_STIPPLE);
  125.     }
  126.     
  127.     if (mode2) {
  128.     ci = CI_OFFSET;
  129.     glEnable(GL_LINE_SMOOTH);
  130.     glEnable(GL_BLEND);
  131.     } else {
  132.     ci = COLOR_YELLOW;
  133.     glDisable(GL_LINE_SMOOTH);
  134.     glDisable(GL_BLEND);
  135.     }
  136.  
  137.     glPushMatrix();
  138.  
  139.     glShadeModel( GL_FLAT );
  140.  
  141.     for (i = 0; i < 360; i += 5) {
  142.     glRotatef(5.0, 0,0,1);
  143.  
  144.     (rgb) ? glColor3f(1.0, 1.0, 0.0) : glIndexi(ci);
  145.     glBegin(GL_LINE_STRIP);
  146.         glVertex3fv(pntA);
  147.         glVertex3fv(pntB);
  148.     glEnd();
  149.  
  150.     glPointSize(1);
  151.  
  152.     SetColor(COLOR_GREEN);
  153.     glBegin(GL_POINTS);
  154.         glVertex3fv(pntA);
  155.         glVertex3fv(pntB);
  156.     glEnd();
  157.     }
  158.  
  159.     glPopMatrix();
  160.  
  161.     glFlush();
  162.  
  163.     if (doubleBuffer) {
  164.     glutSwapBuffers();
  165.     }
  166. }
  167.  
  168. static GLenum Args(int argc, char **argv)
  169. {
  170.     GLint i;
  171.  
  172.     rgb = GL_TRUE;
  173.     doubleBuffer = GL_FALSE;
  174.  
  175.     for (i = 1; i < argc; i++) {
  176.     if (strcmp(argv[i], "-ci") == 0) {
  177.         rgb = GL_FALSE;
  178.     } else if (strcmp(argv[i], "-rgb") == 0) {
  179.         rgb = GL_TRUE;
  180.     } else if (strcmp(argv[i], "-sb") == 0) {
  181.         doubleBuffer = GL_FALSE;
  182.     } else if (strcmp(argv[i], "-db") == 0) {
  183.         doubleBuffer = GL_TRUE;
  184.     } else {
  185.         printf("%s (Bad option).\n", argv[i]);
  186.         return GL_FALSE;
  187.     }
  188.     }
  189.     return GL_TRUE;
  190. }
  191.  
  192. void main(int argc, char **argv)
  193. {
  194.     glutInit(&argc, argv);
  195.  
  196.     if (Args(argc, argv) == GL_FALSE) {
  197.     exit(1);
  198.     }
  199.  
  200.     glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
  201.  
  202.     windType = (rgb) ? GLUT_RGB : GLUT_INDEX;
  203.     windType |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  204.     glutInitDisplayMode(windType);
  205.  
  206.     if (glutCreateWindow("Line Test") == GL_FALSE) {
  207.     exit(1);
  208.     }
  209.  
  210.     InitMap();
  211.  
  212.     Init();
  213.  
  214.     glutReshapeFunc(Reshape);
  215.     glutKeyboardFunc(Key);
  216.     glutDisplayFunc(Draw);
  217.     glutMainLoop();
  218. }
  219.